package com.yflutter.yfree_app.util

import android.annotation.SuppressLint
import com.flutter_yfree.YFlutterPlugin
import com.yflutter.yfree_app.MyApplication
import com.yflutter.yfree_app.activity.H5Activity
import com.yflutter.yfree_app.activity.MainActivity
import com.yfree.flutter.YFlutter
import io.flutter.plugin.common.MethodChannel

@SuppressLint("StaticFieldLeak")
class MyPlugin {
    companion object {
        lateinit var yFlutter: YFlutter

        fun register() {
            yFlutter = YFlutter(MyApplication.mainFlutterActivity)
            MyApplication.mainFlutterActivity = yFlutter.context as MainActivity
            MethodChannel(YFlutterPlugin.flutterEngine.dartExecutor.binaryMessenger, "MyPlugin").setMethodCallHandler { methodCall, result ->
                val arguments = methodCall.arguments as? Map<*, *>
                when (methodCall.method) {
                    "getMsg" ->
                        result.success("欢迎使用YFree_App：android")
                    "goH5" ->
                        yFlutter.yIntent(H5Activity::class.java, null)
                    "goUniapp" -> {
                        MiniAppUtils.goMiniApp(yFlutter, arguments!!["checkVersionJkid"].toString())
                    }
                    else -> result.notImplemented()
                }
            }
        }
    }
}